package hot150;

// https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150
public class q4 {
    public int removeDuplicates(int[] nums) {
        return method1(nums, nums.length, 2);
    }

    /**
     * 双指针 -> 非严格递增
     * 0ms
     * 复杂度O(n)
     */
    public int method1(int[] nums, int n, int mx) {
        int i, j;
        i = j = 0;
        while (j < n) {
            int k = j;
            // 判断重复元素
            while (j < n && nums[j] == nums[k]) {
                // 判断步数是否超过阈值，没操过则赋值
                if (j - k < mx) {
                    nums[i++] = nums[k];
                }
                j++;
            }
        }
        return i;
    }
}
